package cn.jdy.plugins.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.BrandDiscuss;
import cn.jdy.plugins.entity.Company;
import cn.jdy.plugins.entity.CompanyContext;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.BrandDiscussService;
import cn.jdy.plugins.service.CompanyContextService;
import cn.jdy.plugins.service.CompanyService;

/**
 * 品牌评论操作类(品牌评论暂时只允许评论，不允许回复)
 *
 * @author GXP
 */
@Controller
@RequestMapping("/brandDiscuss")
public class BrandDiscussCtrl extends BaseCtrl {
	@Autowired
	private BrandDiscussService brandDiscussService;
	@Autowired
	private CompanyContextService companyContextService;
	@Autowired
	private CompanyService companyService;

	/**
	 * 保存企业展示信息
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(BrandDiscuss discuss, HttpServletRequest request) throws IOException {
		discuss.setUserip(request.getRemoteAddr());
		Member member = super.getMember();
		if(member != null) {
			discuss.setUsername(member.getUserName());
		}
		// 保存评论信息
		discuss.setId(null);
		discuss.setDisscusstime(DateUtil.getCurrentDate2());
		brandDiscussService.save(discuss);
		return "redirect:/brandDiscuss/getPageList/" + discuss.getCompanyid();
	}

	/**
	 * 分页查询品牌评论
	 * @return
	 */
	@RequestMapping("/{companyId}")
	public ModelAndView searchInfo(@PathVariable("companyId") Integer companyId,Integer pageNo, Integer size, String view) {
		ModelAndView mv = new ModelAndView("/company/brandiscuss.jsp");
		mv.addObject("view",view);
		size = size == null?10 :size;
		try {
			Company company = companyService.findOne("id", companyId);
			CompanyContext context = companyContextService.findOne("companyId", companyId);
			mv.addObject("company", company);
			mv.addObject("context", context);

			BrandDiscuss dis =new BrandDiscuss();
			dis.setCompanyid(companyId);
			
			/************* 分页处理开始 ************/
			int total = brandDiscussService.getCount(dis);
			Map<String, Integer> map = getPageInfo(pageNo, size, String.valueOf(total));// 获取page信息
			pageNo = map.get("currentPage");
			size = map.get("pageSize");
			/************* 分页处理结束 ***********/

			dis.setiDisplayStart((pageNo-1)*size);
			dis.setiDisplayLength(size);
			dis.setStatus("1");
			
			List<BrandDiscuss> list = brandDiscussService.query(dis);
			mv.addObject("list", list);
			mv.addObject("pageNo", pageNo);
			mv.addObject("total", total);
			mv.addObject("size", size);
			mv.addObject("companyId", companyId);
			 
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "分页查询品牌评论", e);
		}

		return mv;
	}

}
